﻿using MathNet.Numerics.Distributions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System;
using System.Collections.Generic;
using ztDataNet;
using ztDataNet.db;
namespace ztDataNet.Controllers
{
    /// <summary>
    /// 功法登记
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class NingLiUserController : BaseController<NingLiUser>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db"></param>
        /// <param name="cc"></param>
        public NingLiUserController(ISqlSugarClient db, IDistributedCache cc) : base(db, cc)
        {
            this.db = db;
        }
        /// <summary>
        /// 根据id查询单条
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        // GET: api/Entity/Get/{id}
        [HttpGet("Openid/{code}")]
        public async Task<IActionResult> getOpenid(string code)
        {

            // 如果缓存中没有数据，从数据库中获取数据
            if (string.IsNullOrEmpty(code))
            {
                var redirectUrl = OAuth.GetAuthorizeUrl("wx437cc170f9185234", "回调地址到公网本Action");
                return Ok(redirectUrl);
            }
            else
            {
                //根据code和微信参数得到openid
                var openId = OAuth.GetOpenid("wx437cc170f9185234", "88f80409863285f853d5ac1724d77fb2", code);
                //业务处理
                return Ok(openId);
            }

        }
        /// <summary>
        /// 根据id查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET: api/Entity/Get/{id}
        [HttpGet("GetOpenid/{id}")]
        public async Task<IActionResult> GetEntityByopenid(string id)
        {

            // 如果缓存中没有数据，从数据库中获取数据
            var result = await db.Queryable<NingLiUser>().Where(x=>x.openid == id).SingleAsync();

                // 将数据序列化为JSON
                var serializedResult = JsonConvert.SerializeObject(result);

            // 将数据存储到缓存中，设置滑动过期时间为5分钟

            if (result == null)
            {
                return Ok("no");
            }
            else {
                return Ok(result);
            }
               
            }
        [HttpGet("GetUserByCode/{code}")]
        public async Task<IActionResult> GetUserByCode(string code)
        {

            // 如果缓存中没有数据，从数据库中获取数据
            var result = await db.Queryable<NingLiUser>().Where(x => x.code == code).SingleAsync();

            // 将数据序列化为JSON
            var serializedResult = JsonConvert.SerializeObject(result);

            // 将数据存储到缓存中，设置滑动过期时间为5分钟


            var r = new ResultData<NingLiUser>()
            {
                code = 200,
                msg = "OK",
                data = result
            };

            return Ok(r);


        }
    }
}


    


